package com.yuexun.dhdoctor.module.meetingNew.ui;

import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;


import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseEntity;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.meetingNew.bean.MeetingNewDetailBean;
import com.yuexun.dhdoctor.module.meetingNew.bean.MeetingNewQrCodeBean;
import com.yuexun.dhdoctor.module.meetingNew.model.impl.MeetingNewModel;
import com.yuexun.dhdoctor.module.meetingNew.presenter.impl.MeetingNewGetQrCodePresenter;
import com.yuexun.dhdoctor.module.meetingNew.ui.view.IMeetingNewGetQrCodeView;
import com.yuexun.dhdoctor.util.ShowImageUtils;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wangt on 2018/4/20.
 */
public class Act_MeetingNew_Code extends BaseKJActivity implements IMeetingNewGetQrCodeView {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.img_code)
    ImageView imgCode;

    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.tv_addr)
    TextView tvAddr;

    private SimpleDateFormat daformatter = new SimpleDateFormat("MM-dd HH:mm");
    private SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat timeformatter = new SimpleDateFormat("HH:mm");

    private MeetingNewGetQrCodePresenter getQrCodePresenter;

    private MeetingNewDetailBean meetingBean;
    private String qrcode;
    //刷新时间 单位m
    private int refreshTime;

    private Timer timer;

    @Override
    public void setRootView() {
        super.setRootView();
        setContentView(R.layout.act_meetingnew_code);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        meetingBean = (MeetingNewDetailBean) getIntent().getSerializableExtra("meeting");
        qrcode = getIntent().getStringExtra("qrcode");
        refreshTime = getIntent().getIntExtra("refreshTime", 2);
        getQrCodePresenter = new MeetingNewGetQrCodePresenter(new MeetingNewModel(), this);

        showData();
    }

    @Override
    protected void onStart() {
        super.onStart();
        //运行时，保持屏幕高亮，不锁屏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        if (refreshTime == 0) {
            ApiGetQrCode();
        } else {
            startTimer();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        if (timer != null) {
            timer.cancel();
        }
    }

    //展示数据
    private void showData() {
        tvTitle.setText(meetingBean.getMeetingName());
        tvAddr.setText(meetingBean.getRoomName());
        try {
            Date start = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(meetingBean.getStarttime());
            Date end = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(meetingBean.getEndtime());

            if (dateformatter.parse(meetingBean.getStarttime()).equals(dateformatter.parse(meetingBean.getEndtime()))) {
                tvTime.setText(daformatter.format(start) + " - " + timeformatter.format(end));
            } else {
                tvTime.setText(daformatter.format(start) + " - " + daformatter.format(end));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    //请求二维码
    private void ApiGetQrCode() {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("meetingId", meetingBean.getMeetingId());
        params.put("refreshTime", refreshTime + "");
        getQrCodePresenter.getQrCode(params);
    }

    @Override
    @OnClick({R.id.back, R.id.right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.right:
                if (timer != null) {
                    timer.cancel();
                }

                if (refreshTime == 0) {
                    ApiGetQrCode();
                } else {
                    startTimer();
                }
                break;
        }
    }

    @Override
    public void showQrCode(BaseEntity<MeetingNewQrCodeBean> bean) {
        if (bean.getStatus().equals("200")) {
            //qrCodeStatus==false 不自动刷新
            if (!bean.getDatas().get(0).isQrCodeStatus()) {
                if (timer != null) {
                    timer.cancel();
                }
                ShowImageUtils.showImageView(aty, R.color.white, AppConfig.WEB_QRCODE + qrcode, imgCode);
            } else {
                ShowImageUtils.showImageView(aty, R.color.white, AppConfig.WEB_QRCODE + bean.getDatas().get(0).getQrCodeAddress(), imgCode);
            }
        } else {
            ViewInject.toast(bean.getMessage());
        }
    }

    //开启定时任务
    private void startTimer() {
        timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                ApiGetQrCode();
            }
        };
        timer.schedule(task, 0, (refreshTime * 60 - 5) * 1000);
    }
}
